summaryrefslogtreecommitdiff
path: root/app/api/projects/[projectId]/stats/route.ts
blob: cd17e70d34ffd5a19e695747459c68a483f2b07e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// app/api/fileSystemProjects/[projectId]/stats/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import db from "@/db/db";
import { fileItems, fileActivityLogs, fileSystemProjects, projectMembers } from "@/db/schema";
import { eq, and, gte, sql, desc } from "drizzle-orm";

export async function GET(
  request: NextRequest,
  context: { params: Promise<{ projectId: string }> }
) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return NextResponse.json({ error: '인증이 필요합니다' }, { status: 401 });
    }

    const params = await context.params;
    const projectId = params.projectId;
    
    // URL 파라미터에서 날짜 범위 가져오기
    const searchParams = request.nextUrl.searchParams;
    const range = searchParams.get('range') || '30d';
    
    // 날짜 범위 계산
    const now = new Date();
    let startDate = new Date();
    
    switch (range) {
      case '7d':
        startDate.setDate(now.getDate() - 7);
        break;
      case '30d':
        startDate.setDate(now.getDate() - 30);
        break;
      case '90d':
        startDate.setDate(now.getDate() - 90);
        break;
      default:
        startDate.setDate(now.getDate() - 30);
    }

    // 이전 기간 (트렌드 계산용)
    const previousStartDate = new Date(startDate);
    previousStartDate.setDate(previousStartDate.getDate() - (now.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));

    // 프로젝트 접근 권한 확인
    const projectMember = await db.query.projectMembers.findFirst({
      where: and(
        eq(projectMembers.projectId, projectId),
        eq(projectMembers.userId, Number(session.user.id))
      ),
    });

    const isInternalUser = session.user.domain !== 'partners';
    
    // 내부 사용자가 아니고 프로젝트 멤버가 아닌 경우 접근 거부
    if (!isInternalUser && !projectMember) {
      return NextResponse.json(
        { error: '통계를 볼 권한이 없습니다' },
        { status: 403 }
      );
    }

    // 1. 스토리지 통계
    const storageStats = await db
      .select({
        totalSize: sql<number>`COALESCE(SUM(${fileItems.size}), 0)`,
        fileCount: sql<number>`COUNT(CASE WHEN ${fileItems.type} = 'file' THEN 1 END)`,
        folderCount: sql<number>`COUNT(CASE WHEN ${fileItems.type} = 'folder' THEN 1 END)`,
      })
      .from(fileItems)
      .where(eq(fileItems.projectId, projectId));

    // 카테고리별 파일 수
    const categoryStats = await db
      .select({
        category: fileItems.category,
        count: sql<number>`COUNT(*)`,
      })
      .from(fileItems)
      .where(and(
        eq(fileItems.projectId, projectId),
        eq(fileItems.type, 'file')
      ))
      .groupBy(fileItems.category);

    const byCategory = {
      public: 0,
      restricted: 0,
      confidential: 0,
      internal: 0,
    };

    categoryStats.forEach(stat => {
      if (stat.category && stat.category in byCategory) {
        byCategory[stat.category as keyof typeof byCategory] = Number(stat.count);
      }
    });

    // 2. 활동 통계 (현재 기간)
    const activityStats = await db
      .select({
        action: fileActivityLogs.action,
        count: sql<number>`COUNT(*)`,
      })
      .from(fileActivityLogs)
      .where(and(
        eq(fileActivityLogs.projectId, projectId),
        gte(fileActivityLogs.createdAt, startDate)
      ))
      .groupBy(fileActivityLogs.action);

    // 이전 기간 통계 (트렌드 계산용)
    const previousActivityStats = await db
      .select({
        action: fileActivityLogs.action,
        count: sql<number>`COUNT(*)`,
      })
      .from(fileActivityLogs)
      .where(and(
        eq(fileActivityLogs.projectId, projectId),
        gte(fileActivityLogs.createdAt, previousStartDate),
        sql`${fileActivityLogs.createdAt} < ${startDate}`
      ))
      .groupBy(fileActivityLogs.action);

    const activityCounts = {
      views: 0,
      downloads: 0,
      uploads: 0,
      shares: 0,
    };

    const previousCounts = {
      downloads: 0,
    };

    activityStats.forEach(stat => {
      switch (stat.action) {
        case 'view':
          activityCounts.views = Number(stat.count);
          break;
        case 'download':
          activityCounts.downloads = Number(stat.count);
          break;
        case 'upload':
          activityCounts.uploads = Number(stat.count);
          break;
        case 'share':
          activityCounts.shares = Number(stat.count);
          break;
      }
    });

    previousActivityStats.forEach(stat => {
      if (stat.action === 'download') {
        previousCounts.downloads = Number(stat.count);
      }
    });

    // 트렌드 계산 (다운로드 기준)
    const trend = previousCounts.downloads > 0 
      ? Math.round(((activityCounts.downloads - previousCounts.downloads) / previousCounts.downloads) * 100)
      : 0;

    // 3. 사용자 통계
    const userStats = await db
      .select({
        total: sql<number>`COUNT(DISTINCT ${projectMembers.userId})`,
      })
      .from(projectMembers)
      .where(eq(projectMembers.projectId, projectId));

    // 활성 사용자 (최근 활동이 있는 사용자)
    const activeUsers = await db
      .select({
        count: sql<number>`COUNT(DISTINCT ${fileActivityLogs.userId})`,
      })
      .from(fileActivityLogs)
      .where(and(
        eq(fileActivityLogs.projectId, projectId),
        gte(fileActivityLogs.createdAt, startDate)
      ));

    // 역할별 사용자 수 (간단하게 처리)
    const roleStats = await db
      .select({
        role: projectMembers.role,
        count: sql<number>`COUNT(*)`,
      })
      .from(projectMembers)
      .where(eq(projectMembers.projectId, projectId))
      .groupBy(projectMembers.role);

    const byRole = {
      admin: 0,
      editor: 0,
      viewer: 0,
    };

    roleStats.forEach(stat => {
      if (stat.role === 'owner' || stat.role === 'admin') {
        byRole.admin = Number(stat.count);
      } else if (stat.role === 'editor') {
        byRole.editor = Number(stat.count);
      } else {
        byRole.viewer = Number(stat.count);
      }
    });

    // 4. 최근 활동 내역
    const recentActivities = await db
      .select({
        action: fileActivityLogs.action,
        userEmail: fileActivityLogs.userEmail,
        createdAt: fileActivityLogs.createdAt,
        fileName: fileItems.name,
        fileType: fileItems.type,
      })
      .from(fileActivityLogs)
      .leftJoin(fileItems, eq(fileActivityLogs.fileItemId, fileItems.id))
      .where(and(
        eq(fileActivityLogs.projectId, projectId),
        gte(fileActivityLogs.createdAt, startDate)
      ))
      .orderBy(desc(fileActivityLogs.createdAt))
      .limit(10);

    const recent = recentActivities.map(activity => ({
      type: activity.fileType || 'file',
      user: activity.userEmail?.split('@')[0] || 'Unknown',
      action: activity.action,
      timestamp: activity.createdAt.toISOString(),
      details: activity.fileName || 'Unknown file',
    }));

    // 5. 프로젝트 정보 (스토리지 제한 등)
    const project = await db.query.fileSystemProjects.findFirst({
      where: eq(fileSystemProjects.id, projectId),
    });

    const storageLimit = 10 * 1024 * 1024 * 1024; // 기본 10GB

    // 응답 데이터 구성
    const stats = {
      storage: {
        used: Number(storageStats[0]?.totalSize || 0),
        limit: storageLimit,
        fileCount: Number(storageStats[0]?.fileCount || 0),
        folderCount: Number(storageStats[0]?.folderCount || 0),
        byCategory,
      },
      activity: {
        views: activityCounts.views,
        downloads: activityCounts.downloads,
        uploads: activityCounts.uploads,
        shares: activityCounts.shares,
        trend,
      },
      users: {
        total: Number(userStats[0]?.total || 0),
        active: Number(activeUsers[0]?.count || 0),
        byRole,
      },
      recent,
    };

    return NextResponse.json(stats);

  } catch (error) {
    console.error('통계 조회 오류:', error);
    return NextResponse.json(
      { error: '통계를 불러올 수 없습니다' },
      { status: 500 }
    );
  }
}